﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandBuildLair : BoCommand
    {
        private const float _min = 150.0f;
        private const float _gas = 100.0f;
        private const int _time = 80;
        private const commandID _commandID = commandID.buildLair;

        private int _hatchIndex = -1;
        public BoCommandBuildLair(int hatchIndex)
        {
            _hatchIndex = hatchIndex;
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.removeBuilding((int)Buildings.Hatchery);
            node.gameState.startBuildingBuilding((int)Buildings.Lair);
            node.gameState.getHatchery(_hatchIndex).evolveToLair();

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingBuilding((int)Buildings.Lair);
                n.gameState.getHatchery(_hatchIndex).makeLair();
                n.unlockCommand((int)commandID.buildOverseer);
                n.unlockCommand((int)commandID.buildHydraliskDen);
                n.unlockCommand((int)commandID.buildInfestationPit);
                n.unlockCommand((int)commandID.buildSpire);
                n.unlockCommand((int)commandID.buildNydusNetwork);
                n.unlockCommand((int)commandID.upgradeBurrow);
                n.unlockCommand((int)commandID.upgradeVentralSacs);
                n.unlockCommand((int)commandID.upgradePneumatizedCarapace);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            int nbRequiredLairs = goal.nbBuildingsRequired((int)Buildings.Lair);
            int nbCurrentLairs = node.gameState.nbBuildingsBeingBuilt((int)Buildings.Lair) + node.gameState.nbBuildingsBuilt((int)Buildings.Lair);

            int nbRequiredHives = goal.nbBuildingsRequired((int)Buildings.Hive);
            int nbCurrentHives = node.gameState.nbBuildingsBeingBuilt((int)Buildings.Hive) + node.gameState.nbBuildingsBuilt((int)Buildings.Hive);

            if (nbRequiredLairs == -1)
            {
                if (nbCurrentLairs < 1)
                {
                    return true;
                }
            }
            if (nbRequiredHives == -1)
            {
                if (nbCurrentHives < 1)
                {
                    return true;
                }
            }
            if (nbCurrentLairs < (nbRequiredLairs + (nbRequiredHives - nbCurrentHives)))
            {
                return true;
            }

            node.dismissCommand((int)commandID.buildLair);
            return false;
        }
        public static bool isAvailable(BoGameState gs, int hatchIndex)
        {
            if (!gs.getHatchery(hatchIndex).isHatchery)
            {
                return false;
            }
            if (gs.getHatchery(hatchIndex).isEvolving)
            {
                return false;
            }
            if (gs.getHatchery(hatchIndex).isBuildingQueen)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, 0))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100) + _hatchIndex;
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Hatchery " + _hatchIndex + " -> Lair";
            return str;
        }
    }
}
